package com.jwj.controller;

import com.jwj.pojo.Category;
import com.jwj.pojo.CategoryTable;
import com.jwj.pojo.Category_account;
import com.jwj.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/category")
//@CrossOrigin(origins="*",maxAge = 3600)
public class CategoryContrller {

    @Autowired
    private CategoryService categoryService;
    //传json
    //查询所有
    @GetMapping( value = "/all")
    public @ResponseBody List<Category_account> CategoryAll(){
        List<Category_account> categories= categoryService.CategoryAll();
//        System.out.println(categories);
        return categories;
    }
//    查单表，select值
    @GetMapping(value = "/select")
    public @ResponseBody List<CategoryTable> CategoryTable(){
        List<CategoryTable> categoryTables= categoryService.CategoryTable();
//        System.out.println(categoryTables);
        return categoryTables;
    }
    //查询状态
    @GetMapping(value = "/showState")
    public @ResponseBody List<Category_account> toFindState(String state){
        List<Category_account> categories = categoryService.toFindState(state);
        return categories;
    }
    //添加一个分类
    @RequestMapping(value = "/add")
    public @ResponseBody int AddCategory(Category category){
        int i = categoryService.AddCategory(category);
        System.out.println(i);
        return i;
    }
    //删除一个分类
    @GetMapping(value = "/delete")
    @ResponseBody
    public int DeleteCategory(int category_id){
        int i = categoryService.DeleteCategory(category_id);
        System.out.println(i);
        return i;
    }
    //找到分类信息
    @GetMapping("/tofind")
    public @ResponseBody Category toFindCategory(int category_id){
        Category category = categoryService.toFindCategory(category_id);
        return category;
    }
    //修改一个分类
    @RequestMapping(value = "/update")
    public @ResponseBody int UpdateCategory(Category category){
        int i = categoryService.UpdateCategory(category);
        System.out.println(i);
        return i;
    }
//    public ModelAndView toFindCategory(int category_id,ModelAndView model){
//        Category categoryList = categoryService.toFindCategory(category_id);
//        model.addObject("categoryOne",categoryList);
//        System.out.println(categoryList);
//        model.setViewName("/update_post_category");
//        return model;
//    }

    //显示所有信息 model
//    @GetMapping( value = "/all")
//    public ModelAndView CategoryAll(ModelAndView model){
//        Collection<Category> categories = categoryService.CategoryAll();
//        model.addObject("categoryList",categories);
//        for (Category a :categories){
//            System.out.println(a.toString());
//        }
//        model.setViewName("/post_category");
//        //return "redirect:/post_category.html";
//        return model;
//    }
}